package com.ruoyi.web.controller.erp.conventionaldata;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.page.TableDataInfo;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.erp.conventionaldata.domain.ScrappingCauseTab;
import com.ruoyi.erp.conventionaldata.mapper.ScrappingCauseTabMapper;
import com.ruoyi.erp.conventionaldata.service.ScrappingCauseTabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

@RequestMapping("scrappingCause")
@RestController
@CrossOrigin
public class ScrappingCauseTabController extends BaseController {
    @Autowired
    private ScrappingCauseTabService scrappingCauseTabService;


    @Autowired
    private ScrappingCauseTabMapper scrappingCauseTabMapper;




    @GetMapping("/list")
    public TableDataInfo list(ScrappingCauseTab scrappingCauseTab){
        List<ScrappingCauseTab> list = scrappingCauseTabService.getAllScrappingCauseTab(scrappingCauseTab);
        //list.stream().forEach(le -> le.setRowversion(DateUtils.parseDate(le.getRowversion())));
        return  getDataTable(list);
    }


    @PostMapping
    public AjaxResult add(@RequestBody ScrappingCauseTab scrappingCauseTab){
        scrappingCauseTab.setRowkey(UUID.randomUUID().toString());
        if (scrappingCauseTab.getRowversion() == null){
            scrappingCauseTab.setRowversion(new Date());
        }
        scrappingCauseTabService.add(scrappingCauseTab);
        return toAjax(1);
    }

    @PutMapping
    public AjaxResult update(@RequestBody ScrappingCauseTab scrappingCauseTab){
        scrappingCauseTabService.update(scrappingCauseTab);
        return toAjax(1);
    }

    @DeleteMapping("/{rowkey}")
    public AjaxResult delete(@PathVariable String rowkey[]){

        scrappingCauseTabService.delete(rowkey);
        return toAjax(1);
    }


    @GetMapping("/{rowkey}")
    public AjaxResult getInfoById(@PathVariable String rowkey ){
        return AjaxResult.success(scrappingCauseTabService.getScrappingCauseTabById(rowkey));
    }

    @PostMapping("/export")
    public AjaxResult export(@RequestBody List<String> ids){

        ExcelUtil<ScrappingCauseTab> util = new ExcelUtil<ScrappingCauseTab>(ScrappingCauseTab.class);
        ScrappingCauseTab scrappingCauseTab = new ScrappingCauseTab();
        List<ScrappingCauseTab>list = ids.size() == 0? scrappingCauseTabService.getAllScrappingCauseTab(scrappingCauseTab):scrappingCauseTabMapper.selectBatchIds(ids);
        AjaxResult ajaxResult = util.exportExcel(list, "报废原因");
        logger.info(ajaxResult.toString());
        return ajaxResult;
    }




}
